#!/usr/bin/env python

import sys
import os


systemAPP = "google-chrome"
chromeExtensionApp = "ijdncbaekoleeeddhkbnoihmmlfalmkb"
pathConfg = ".tvCTRC"
fileExtAPP = "chromeExtensionApp"
fileDefAPP = "chromeApp"

def getUrlAppScript(name):
	app = systemAPP + " --app=\"chrome-extension://" + chromeExtensionApp + "/canal/script.html#" + name + "\""
	return app

def getUrlAppFlash(name):
	app = systemAPP + " --app=\"chrome-extension://" + chromeExtensionApp + "/canal/flash.html#" + name + "\""
	return app

def ConfigDirPath():
	dirConfgRaiz = os.path.join(os.path.expanduser("~"), pathConfg)
	if not os.path.isdir( dirConfgRaiz ) :
		os.mkdir(dirConfgRaiz)
		print("Directorio : "+dirConfgRaiz + " creado.")

#define y recupera el id del la palicacion de chrome
def fileExtensionApp(ExtApp = None) :
	filePathOpen = os.path.join(os.path.expanduser("~"), pathConfg, fileExtAPP)
	if ExtApp == None :
		if not os.path.isfile(filePathOpen) :
			f = open(filePathOpen,"w")
			f.write(chromeExtensionApp)
			f.close()
		f = open(filePathOpen,"r")
		contentfile = f.read()
		f.close()
		return contentfile
	else :
		f = open(filePathOpen,"w")
		f.write(ExtApp)
		f.close()
		f = open(filePathOpen,"r")
		contentfile = f.read()
		f.close()
		return contentfile

#define y recupera el nombre de la aplicacion de chrome
def fileChromeApp(App = None) :
	filePathOpen = os.path.join(os.path.expanduser("~"), pathConfg, fileDefAPP)
	if App == None :
		if not os.path.isfile(filePathOpen) :
			f = open(filePathOpen,"w")
			f.write(chromeExtensionApp)
			f.close()
		f = open(filePathOpen,"r")
		contentfile = f.read()
		f.close()
		return contentfile
	else :
		f = open(filePathOpen,"w")
		f.write(App)
		f.close()
		f = open(filePathOpen,"r")
		contentfile = f.read()
		f.close()
		return contentfile

def startTv(nameParam):
	## identifica esepciones de coincidencias numericas
	if nameParam == "13" :
		nameParam = "Canal 13"
	elif nameParam == "14" :
		nameParam = "13C"
	elif nameParam == "9" :
		nameParam = "Mega"
	elif nameParam == "11" :
		nameParam = "CHV"
	elif nameParam == "6" :
		nameParam = "CNN Chile"
	elif nameParam == "5" :
		nameParam = "UCV"
	elif nameParam == "4" :
		nameParam = "La Red"

	# ejecucion de programa
	if nameParam == "" or nameParam == "UCV" or nameParam == "CNN Chile" or nameParam == "La Red" :
		ejecucion = getUrlAppFlash(nameParam)
	else :
		ejecucion = getUrlAppScript(nameParam)

	# Ejecuta en navegador con la sintonizasion
	os.system(ejecucion)


Param = ""
Comand = list()
# agrupa parametros
if len(sys.argv) > 1 :
	conteo = 0
	while conteo < len(sys.argv):
		if sys.argv[conteo] != sys.argv[0] :
			## asigna comandos agregados
			if sys.argv[conteo][0] == "-" :
				Comand.append(sys.argv[conteo])				
				conteo = conteo + 1
				try:
					Comand.append(sys.argv[conteo])
				except Exception :
					print(Exception)
			else:
				Param += sys.argv[conteo]
		try :
			if sys.argv[conteo] != sys.argv[len(sys.argv) - 1] and sys.argv[conteo] != sys.argv[0] :
				Param += " "
		except Exception :
			print(Exception)
		## incremente contador
		conteo = conteo + 1

##configuracion
ConfigDirPath()
chromeExtensionApp = fileExtensionApp()
systemAPP = fileChromeApp()

contador = 0
while contador < len(Comand) :
	if Comand[contador] == "-e" or Comand[contador] == "--extension":
		contador = contador + 1
		if contador < len(Comand) :
			chromeExtensionApp = fileExtensionApp(Comand[contador])
	if Comand[contador] == "-a" or Comand[contador] == "--chrome-app":
		contador = contador + 1
		if contador < len(Comand) :
			systemAPP = fileChromeApp(Comand[contador])
	if Comand[contador] == "-h" or Comand[contador] == "--help":
		exit()
	if Comand[contador] == "-n" or Comand[contador] == "--not-run":
		exit()
	contador = contador + 1

if Param.upper() == "F" or Param == "" :
	#Establesido por fefecto 
	startTv("13")
	startTv("6")
	
else :
	#ejecucion normal
	startTv(Param)



